package com.cn.serverline.lineicp.workshopPlan.util;

import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.payload.DefaultJwtPayload;
import cn.stylefeng.roses.kernel.auth.jwt.AuthJwtTokenService;
import cn.stylefeng.roses.kernel.sys.api.SysUserServiceApi;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.UserValidateDTO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @program: AssembleIcpCode
 * @ClassName TokenRemoteUtil
 * @author: ayetony miao
 * @create: 2023-12-21 18:01
 **/
@Component
public class TokenRemoteUtil {


    @Resource
    private SessionManagerApi sessionManagerApi;

    @Resource
    private AuthJwtTokenService authJwtTokenService;

    @Resource
    private SysUserServiceApi sysUserServiceApi;

    public LoginUser getUserIdByToken(String token) {
        LoginUser session = sessionManagerApi.getSession(token);
        if(session == null){
            throw new RuntimeException("session 不存在");
        }
        return session;
    }

    /**
     * 可用 token 中获取 user 信息
     */
    public UserValidateDTO getCurrentUserId(String validToken){
        DefaultJwtPayload defaultPayload = authJwtTokenService.getDefaultPayload(validToken);
        // 获取用户的信息
        String account = defaultPayload.getAccount();
        // 获取用户信息
        return sysUserServiceApi.getUserLoginValidateDTO(account);
    }

}
